<?php

namespace App\Imports;


use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use stdClass;
use App\Models\DeptModel;
use App\Models\AdminRoleModel;

class FixedImport implements ToCollection, WithMultipleSheets
{


    private $result;

    public function __construct() {
        $this->result = new stdClass;
    }

    public function collection(Collection $rows){
        $lines = [];
        $this->result->error = [];

        $data = [];
        foreach ($rows as $k => $row){
            if($k == 0) continue;
            $one = [];
            foreach ($row as $k2 => $v){
                $one[$rows[0][$k2]] = $v;
            }
            $data[] = $one;
        }



        foreach ($data as $k => $v){
            if(is_int($v['购置日期'])){
                $buy_date = $v['购置日期'] ? Date::excelToDateTimeObject($v['购置日期'])->format('Y-m-d') : NULL;
            }else{
                $buy_date  = $v['购置日期'];
            }
            if(is_int($v['入账日期'])){
                $enter_date = $v['入账日期'] ? Date::excelToDateTimeObject($v['入账日期'])->format('Y-m-d') : NULL;
            }else{
                $enter_date  = $v['入账日期'];
            }
            if(is_int($v['到期日'])){
                $end_date = $v['到期日'] ? Date::excelToDateTimeObject($v['到期日'])->format('Y-m-d') : NULL;
            }else{
                $end_date  = $v['到期日'];
            }
            if(is_int($v['领用日期'])){
                $take_date = $v['领用日期'] ? Date::excelToDateTimeObject($v['领用日期'])->format('Y-m-d') : NULL;
            }else{
                $take_date  = $v['领用日期'];
            }

            $lines[] = [
                'code' => $v['资产编码'],
                'name' => $v['资产名称'],
                'category' => $v['资产类别'],
                'spe' => $v['规格'],
                'model' => $v['品牌/型号'],
                'unit' => $v['单位'],
                'color' => $v['颜色'],
                'buy_date' => $buy_date,
                'supplier' => $v['供应商'],
                'repair_cycle' => $v['保修周期'],
                'enter_date' => $enter_date,
                'pre_year' => $v['使用年限'],
                'original_value' => $v['原值'],
                'value_rate' => $v['残值率'],
                'pre_value' => $v['预计残值'],
                'end_date' => $end_date,
                'depreciation_month' => $v['折旧月数'],
                'depreciation_amount_month' => $v['月折旧'],
                'used_month' => $v['已提月份'],
                'depreciation_amount' => $v['累计折旧'],
                'clean_value' => $v['资产净值'],
                'status' => $v['资产状态'],
                'take_date' => $take_date,
                'user_dept' => $v['所属部门'],
                'user' => $v['领用人员'],
                'place' => $v['存放地点'],
                'remarks' => $v['备注'],
            ];
        }

//        foreach ($rows as $k => $row) {
//            if($k == 0) continue;
//
//            $lines[] = [
//                'code' => $row[1],
//                'name' => $row[2],
//                'category' => $row[3],
//                'type' => $row[4],
//                'model' => $row[5],
//                'unit' => $row[6],
//                'buy_date' => $row[7],
//                'supplier' => $row[8],
//                'repair_cycle' => $row[9],
//                'enter_date' => $row[10],
//                'original_value' => $row[11],
//                'loss_cycle' => $row[12],
//                'value_rate' => $row[13],
//                'depreciation_value' => $row[14],
//                'depreciation_amount' => $row[15],
//                'depreciation_amount_year' => $row[16],
//                'depreciation_amount_month' => $row[17],
//                'leave_cycle' => $row[18],
//                'clean_value' => $row[19],
//                'status' => $row[20],
//                'take_date' => $row[21],
//                'user_dept' => $row[22],
//                'user' => $row[23],
//                'place' => $row[24],
//                'remarks' => $row[25],
//            ];
//        }

        $this->result->lines = $lines;
    }

    public function sheets(): array {
        return [ 0 => $this];
    }

    public function get_result(){
        return $this->result;
    }


}
